#include <iostream>
#include <vector>
using namespace std;

// 链表节点定义
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

// 反转链表指定区间函数
ListNode* reverseBetween(ListNode* head, int left, int right) {
    ListNode* dummy = new ListNode(0);
    dummy->next = head;

    ListNode* pre = dummy;
    for (int i = 1; i < left; i++) {
        cout << "pre->value : " << pre->val << endl;
        cout << "i : " << i << endl;
        cout << "left : " << left << endl;
        pre = pre->next;
    }

    ListNode* cur = pre->next;
    ListNode* next = nullptr;
    for (int i = 0; i < right - left; i++) {
        next = cur->next;
        cur->next = next->next;
        next->next = pre->next;
        pre->next = next;
    }

    return dummy->next;
}

// 打印链表函数
void printList(ListNode* head) {
    while (head) {
        cout << head->val;
        if (head->next) cout << " -> ";
        head = head->next;
    }
    cout << endl;
}

// 构造链表函数
ListNode* createList(const vector<int>& vals) {
    ListNode* dummy = new ListNode(0);
    ListNode* cur = dummy;
    for (int val : vals) {
        cur->next = new ListNode(val);
        cur = cur->next;
    }
    return dummy->next;
}

// 主函数调用示例
int main() {
    // 构造链表
    vector<int> vals = {1, 2, 3, 4, 5};
    ListNode* head = createList(vals);

    cout << "Original list: ";
    printList(head);

    // 调用反转函数
    int left = 2, right = 4;
    head = reverseBetween(head, left, right);

    cout << "Reversed list: ";
    printList(head);

    return 0;
}
